<template>
  <div class="pay">
    <van-nav-bar title="订单详情" left-arrow @click-left="$router.go(-1)" />

    <div class="address">
      <div class="left">
        <van-icon name="logistics" />
        <div class="area" v-if="selectedAddress.address_id">
          <p>{{ list[0].name }} {{ list[0].phone }}</p>
          <p>{{ longAddress }}</p>
        </div>
        <div v-else>请选择配送地址</div>
      </div>
      <van-icon name="arrow" />
    </div>

    <div class="order-list">
      <div class="order-item" v-for="item in order.goodsList" :key="item.goods_id">
        <div class="img">
          <img :src="item.goods_image" alt="">
        </div>
        <div class="info">
          <div class="tit text-ellipsis-2">{{ item.goods_name }}</div>
          <div class="num">
            <span class="count">x{{ item.total_num }}</span>
            <span class="price">¥{{ item.total_pay_price }}</span>
          </div>
        </div>
      </div>
    </div>
    <div class="flow-num-box">
      <span>共 {{ order.orderTotalNum }}件商品，合计：</span>
      <span class="money">¥{{ order.orderTotalPrice }}</span>
    </div>
    <div class="pay-detail">
      <div class="pay-cell">
        <span>订单总金额：</span>
        <span class="red">¥ {{ order.orderTotalPrice }}</span>
      </div>
      <div class="pay-cell">
        <span>优惠券：</span>
        <span>无优惠券可用</span>
      </div>
      <div class="pay-cell">
        <span>配送费用：</span>
        <span v-if="!selectedAddress">请先选择配送地址</span>
        <span v-else class="red">+¥0.00</span>
      </div>
    </div>

     <!-- 支付方式 -->
     <div class="pay-way">
      <span class="tit">支付方式</span>
      <div class="pay-cell">
        <span><van-icon name="balance-o" />余额支付（可用 ¥ {{ personal.balance }} 元）</span>
        <!-- <span>请先选择配送地址</span> -->
      <span class="red"><van-icon name="passed" /></span>
      </div>
     </div>

      <!-- 买家留言 -->
      <div class="buytips">
        <textarea name="" id="" v-model="remark" placeholder="选填：买家留言（50字内）"
        cols="30" rows="10"></textarea>
      </div>

      <!-- 底部提交 -->
      <div class="footer-fixed">
        <div class="left">实付款：<span>¥{{ order.orderTotalPrice }}</span></div>
        <div class="tipsbtn" @click="submitOrder">提交订单</div>
      </div>
  </div>
</template>

<script>
import { getAddressList } from '@/api/address'
import { checkOrder, submitOrder } from '@/api/order'

export default {
  name: 'PayPage',
  data () {
    return {
      list: [],
      order: {},
      personal: {},
      remark: ''
    }
  },
  async created () {
    this.getAddressList()
    this.getOrderList()
  },
  computed: {
    selectedAddress () {
      return this.list[0] || {}
    },
    longAddress () {
      const region = this.list[0].region
      return region.province + region.city + this.list[0].detail
    },
    mode () {
      return this.$route.query.mode
    },
    cartIds () {
      return this.$route.query.cartIds
    },
    goodsId () {
      return this.$route.query.goodsId
    },
    goodsSkuId () {
      return this.$route.query.goodsSkuId
    },
    goodsNum () {
      return this.$route.query.goodsNum
    }
  },
  methods: {
    async getAddressList () {
      const { data: { list } } = await getAddressList()
      this.list = list
    },
    async getOrderList () {
      if (this.mode === 'cart') {
        const { data: { order, personal } } = await checkOrder(this.mode, {
          cartIds: this.cartIds
        })
        this.order = order
        this.personal = personal
      }
      if (this.mode === 'buyNow') {
        const { data: { order, personal } } = await checkOrder(this.mode, {
          goodsId: this.goodsId,
          goodsSkuId: this.goodsSkuId,
          goodsNum: this.goodsNum
        })
        this.order = order
        this.personal = personal
      }
    },
    async submitOrder () {
      if (this.mode === 'cart') {
        await submitOrder(this.mode, {
          cartIds: this.cartIds,
          remark: this.remark
        })
      }
      if (this.mode === 'buyNow') {
        await submitOrder(this.mode, {
          goodsId: this.goodsId,
          goodsSkuId: this.goodsSkuId,
          goodsNum: this.goodsNum,
          remark: this.remark
        })
      }
      this.$toast.success('支付成功')
      this.$router.replace('/order')
    }
  }
}
</script>

<style lang="less" scoped>
.address {
  display: flex;
  padding: 15px;
  justify-content: space-between;
  align-items: center;
  background: url(@/assets/border-line.png) bottom repeat-x;
  background-size: 60px;
  .left {
    display: flex;
    justify-content: center;
    align-items: center;
    padding-left: 5px;
    .van-icon {
      margin-right: 20px;
      font-size: 24px;
      color: #444;
    }
    p {
      color: #444;
      font-size: 14px;
    }
  }
}
.order-list {
  .order-item {
    padding: 10px;
    display: flex;
    align-items: center;
    .img {
     width: 100px;
     img {
      display: block;
      width: 80px;
     }
    }
    .info {
      flex: 1;
      font-size: 14px;
      line-height: 1.3;
      display: flex;
      flex-direction: column;
      justify-content: space-evenly;
      color: #333;
      .num {
        margin-top: 8px;
        display: flex;
        justify-content: space-between;
        .price {
          color: #fa2209;
        }
      }
    }
  }
}
.flow-num-box {
  display: flex;
  justify-content: flex-end;
  padding: 10px  10px;
  font-size: 14px;
  border-bottom: 1px solid #efefef;
  align-items: center;
  .money {
    color: #fa2209;
  }
}
.pay-cell {
  font-size: 14px;
  padding: 10px 12px;
  color: #333;
  display: flex;
  justify-content: space-between;
  .red {
    color: #fa2209;
  }
}
.pay-detail {
  border-bottom: 1px solid #efefef;
}
.pay-way {
  font-size: 14px;
  padding: 10px 12px;
  border-bottom: 1px solid #efefef;
  color: #333;
  .tit {
    line-height: 30px;
  }
  .pay-cell {
    padding: 10px 0;
  }
  .van-icon {
    font-size: 20px;
    margin-right: 5px;
  }
}
.buytips {
  display: block;
  textarea {
    display: block;
    width: 100%;
    border: none;
    font-size: 14px;
    padding: 12px;
    height: 100px;
    border-bottom: 1px solid #efefef
  }
}
.footer-fixed {
  position: fixed;
  background-color: #fff;
  left: 0;
  bottom: 0;
  width: 100%;
  height: 46px;
  line-height: 46px;
  border-top: 1px solid #efefef;
  font-size: 14px;
  display: flex;
  .left {
    flex: 1;
    padding-left: 12px;
    color: #666;
    span {
      color:#fa2209;
    }
  }
  .tipsbtn {
    width: 121px;
    background: linear-gradient(90deg,#f9211c,#ff6335);
    color: #fff;
    text-align: center;
    line-height: 46px;
    display: block;
    font-size: 14px;
  }
}
</style>
